package com.yz.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.util.StringUtils;

public class StringUtil {

	public static String randomUUID(){
		UUID uuid= UUID.randomUUID();
		String id=uuid.toString().replaceAll("-", "");
		return id;
	}
	
	public static String joinIntList(List<Integer> intList){
		if(intList==null || intList.size()==0){
			return null;
		}else{
			StringBuffer sb=new StringBuffer();
			for(int i=0;i<intList.size();i++){
				Integer value=intList.get(i);
				if(i==0){
					sb.append(""+value);
				}else{
					sb.append(","+value);
				}
			}
			return sb.toString();
		}
	}
	
	public static String joinStringIdsNoQuote(String[] ids){
		if(ids!=null && ids.length>0){
			StringBuffer sb=new StringBuffer();
			for(int i=0;i<ids.length;i++){
				if(i==0){
					sb.append(""+ids[i]+"");
				}else{
					sb.append(","+ids[i]+"");
				}
			}
			return sb.toString();
		}else{
			return "";
		}
	}
	
	public static String joinStringIds(String[] ids){
		if(ids!=null && ids.length>0){
			StringBuffer sb=new StringBuffer();
			for(int i=0;i<ids.length;i++){
				if(i==0){
					sb.append("'"+ids[i]+"'");
				}else{
					sb.append(",'"+ids[i]+"'");
				}
			}
			return sb.toString();
		}else{
			return "";
		}
	}
	
	public static String joinStringIdList(List<String> idList){
		if(idList!=null && idList.size()>0){
			StringBuffer sb=new StringBuffer();
			for(int i=0;i<idList.size();i++){
				if(i==0){
					sb.append("'"+idList.get(i)+"'");
				}else{
					sb.append(",'"+idList.get(i)+"'");
				}
			}
			return sb.toString();
		}else{
			return "";
		}
	}
	
	public static String joinStringIdListNoQuote(List<String> idList){
		if(idList!=null && idList.size()>0){
			StringBuffer sb=new StringBuffer();
			for(int i=0;i<idList.size();i++){
				if(i==0){
					sb.append(idList.get(i));
				}else{
					sb.append(","+idList.get(i));
				}
			}
			return sb.toString();
		}else{
			return "";
		}
	}
	
	public static String joinStringIdSet(Set<String> idSet){
		if(idSet!=null && idSet.size()>0){
			String idStr="";
			for(String id : idSet){
				if(!StringUtils.isEmpty(id)){
					idStr+=",'"+id+"'";
				}
			}
			if(idStr.length()>0){
				return idStr.substring(1);
			}else{
				return "";
			}
		}else{
			return "";
		}
	}
	
	/**
	 * 剔除两头空格，剔除两头的全角空格和unicode字符'\u0000'到'\u0020'所有空白字符。
	 * @param str
	 * @return
	 */
	public static String trim(String str){
		if(str==null || str.length()==0){
			return str;
		}
		str=str.trim();
		if(StringUtils.isEmpty(str)){
			return str;
		}
		int start=0;
		int end=0;
		for(int i=0;i<str.length();i++){
			if(str.charAt(i)=='　' || (str.charAt(i)>='\u0000' && str.charAt(i)<='\u0020')){
				continue;
			}else{
				start=i;
				break;
			}
		}
		for(int j=str.length()-1;j>=0;j--){
			if(str.charAt(j)=='　' || (str.charAt(j)>='\u0000' && str.charAt(j)<='\u0020')){
				continue;
			}else{
				end=j;
				break;
			}
		}
		if(start>end){
			return "";
		}else{
			return str.substring(start,end+1);
		}
	}
	
	public static String changeToHtml(String str){
		if(StringUtils.isEmpty(str)){
			return str;
		}
		str=str.replaceAll(" ", "&nbsp;");
		str=str.replaceAll("\r\n", "<br/>");
		str=str.replaceAll("\n", "<br/>");
		return str;
	}
	
	public static String changeHtmlForShow(String str){
		if(StringUtils.isEmpty(str)){
			return str;
		}
		str=str.replaceAll("<br/>", "\r\n");
		str=str.replaceAll("&nbsp;", " ");
		return str;
	}
	
	/**
	 * 替换指定位置的子串为*
	 * @param srcStr
	 * @param startIndex
	 * @param endIndex
	 * @return
	 */
	public static String replaceSubStringStar(String srcStr,int startIndex, int endIndex){
		if(StringUtils.isEmpty(srcStr)){
			return "";
		}
		if(startIndex>=endIndex){
			return srcStr;
		}
		if(startIndex<0 || startIndex>=srcStr.length() || endIndex<0){
			return srcStr;
		}
		if(endIndex>srcStr.length()){
			endIndex=srcStr.length();
		}
		char[] charArray=srcStr.toCharArray();
		for(int i=startIndex;i<endIndex;i++){
			try{
				charArray[i]='*';
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		srcStr=new String(charArray);
		return srcStr;
	}
	
	public static void main(String[] args){
		NumberFormat numForm=new DecimalFormat("00");
//			System.out.println("["+'\u0000'+"]");
//			System.out.println("["+'\u0001'+"]");
//			System.out.println("["+'\u0002'+"]");
//			System.out.println("["+'\u0003'+"]");
//			System.out.println("["+'\u0004'+"]");
//			System.out.println("["+'\u0005'+"]");
//			System.out.println("["+'\u0006'+"]");
//			System.out.println("["+'\u0007'+"]");
//			System.out.println("["+'\u0008'+"]");
//			System.out.println("["+'\u0009'+"]");
//			System.out.println("["+'\u0010'+"]");
//			System.out.println("["+'\u0011'+"]");
//			System.out.println("["+'\u0012'+"]");
//			System.out.println("["+'\u0013'+"]");
//			System.out.println("["+'\u0014'+"]");
//			System.out.println("["+'\u0015'+"]");
//			System.out.println("["+'\u0016'+"]");
//			System.out.println("["+'\u0017'+"]");
//			System.out.println("["+'\u0018'+"]");
//			System.out.println("["+'\u0019'+"]");
//			System.out.println("["+'\u001A'+"]");
//			System.out.println("["+'\u001B'+"]");
//			System.out.println("["+'\u001C'+"]");
//			System.out.println("["+'\u001D'+"]");
//			System.out.println("["+'\u001E'+"]");
//			System.out.println("["+'\u001F'+"]");
//			System.out.println("["+'\u0020'+"]");
			
			System.out.println("abc".substring(1, 1));
			System.out.println(Integer.toHexString((int)('　')));
			System.out.println("["+'\u3000'+"]");
	}
}
